<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Accounts extends Model
{
    public function getaccounts($res)
    {
        $page = $res['page'];
        $limit = $res['pageSize'];
        $data = $this->page($page, $limit)->select();
        foreach ($data as $datakey => $dataval) {
            $data[$datakey]['banknames'] = getinfo($dataval['banks_id'], 'banks')['name'];
            //授权员工
            $maparr['id'] = ['in', $dataval['admin_ids']];
            $data[$datakey]['managename'] = implode('/', array_column(getables('admin', $maparr), 'nickname'));
            $data[$datakey]['countryname'] = getinfo($dataval['country_id'], 'country')['name'];
            $count = explode(',', $dataval['is_payment']);
            if (count($count) == 2) {
                $data[$datakey]['is_payment'] = lang("collection/payment");
            } elseif (count($count) == 1) {
                if (in_array('1',$count)) {
                    $data[$datakey]['is_payment'] = lang("collection");
                } elseif (in_array('2',$count)) {
                    $data[$datakey]['is_payment'] = lang("payment");
                }
            } else {
                $data[$datakey]['is_payment'] = '';
            }
        }
        $count = $this->count();
        return array('count' => $count, 'data' => $data);
    }

    public function addeditaccounts($data)
    {
        $ckdata = notEmpty(['country_id' => lang("country"), 'banks_id' => lang("bank"), 'bankname' => lang("account_opening_bank_branch"), 'account_name' => lang("account_name"), 'account_num' => lang("account")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }

        if (!isset($data['is_main'])) {
            $data['is_main'] = '0';
        }
        if (!isset($data['is_public'])) {
            $data['is_public'] = '0';
        }
        if (isset($data['is_payment'])) {
            $ispayment = $data['is_payment'] = implode(",", $data['is_payment']);
        } else {
            $ispayment = $data['is_payment'] = '';
        }
        //一个收款账号 一个付款账号 收款和付款只有一个主要
        if ($ispayment) {
            //表示这个收款账号和付款账号已存在
            $infoispay = $this->where('is_payment', $ispayment)->find();
            if ($infoispay) {
                $this->where('is_payment', $ispayment)->setField('is_main', 0);
            }
        }
        $act = $data['act'];
        unset($data['act']);
        if ($act == 'accountsadd') {
            if (!$data['admin_ids']) {
                return array('msg' => lang("employee_authorized_can't_be_empty"), 'status' => 2);
            }
            unset($data['val_zh']);
            unset($data['id']);
            $data['create_id'] = $data['update_id'] = session('userid');
            $data['create_at'] = $data['update_at'] = time();
            $accountsid = $this->insert($data);
            if ($accountsid) {
                return array('msg' => $data['account_name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['account_name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'accountsedit') {
            unset($data['val_zh']);
            //判断是否是全球管理员
            $admin = session('admin');
            if ($admin['position_id'] != 1) {
                $country_id = getinfo($admin['position_id'], 'position')['country_id'];
                $posid = array_column(getFrames('position', null, $country_id), 'id');
                $mappos['position_id'] = ['in', $posid];
                $datapos = array_column(getables('admin', $mappos), 'id');
                $infodata = explode(',', $this->where('id', $data['id'])->value('admin_ids'));//数据库已存的授权id
                foreach ($infodata as $infodatakey => $infodataval) {
                    if (in_array($infodataval, $datapos)) {
                        unset($infodata[$infodatakey]);
                    }
                }
                $authids = explode(',', $data['admin_ids']);
                $authidsarr = array_unique(array_filter(array_merge($infodata, $authids)));
                $data['admin_ids'] = implode(',', $authidsarr);
            }
            $data['update_id'] = session('userid');
            $data['update_at'] = time();
            $editban = $this->where('id', $data['id'])->update($data);
            if ($editban !== false) {
                return array('msg' => $data['account_name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['account_name'] . lang("edit_failed"), 'status' => 0);
            }
        }

    }

    //收款账号的删除
    public function delaccounts($data)
    {
        $accountname = getinfo($data['id'], 'accounts')['account_name'];
        $delid = $this->where('id', $data['id'])->delete();
        if ($delid) {
            return array('msg' => $accountname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $accountname . lang("failed_to_delete"), 'status' => 0);
        }
    }
}